package com.sicheng.algorithm.string;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/1/24 12:08
 */
public class 字符流中第一个不重复的字符 {
    public static void main(String[] args) {
        char res = firstNotRepeatable("abca");
        System.out.println(res);
    }

    public static char firstNotRepeatable(String str) {
        if (str == null || str.length() == 0)
            throw new RuntimeException();

        LinkedHashMap<Character, Boolean> map = new LinkedHashMap<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!map.containsKey(c)) {
                map.put(c, true);
            } else {
                map.put(c, false);
            }
        }

        char res = 0;
        for (Map.Entry<Character, Boolean> entry : map.entrySet()) {
            if (entry.getValue()) {
                res = entry.getKey();
                break;
            }
        }

        return res;
    }
}
